package com.proxyhub.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

/**
 * Created by linxsh on 2017/6/15.
 */
public class RequestUtil {

    /**
     * 发送get请求
     * @param url
     * @param charset
     * @return
     */
    public static String doGet(String url, String charset) {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setUserAgent(CommonUtil.USERAGENT).build();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode == 200) {
                return EntityUtils.toString(response.getEntity(), charset);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                }
            }
        }
        return null;
    }

    public static Document doGetDoc(String url, String charset) {
        String result = doGet(url, charset);
        if(StringUtils.isNotBlank(result)) {
            return Jsoup.parse(result, url);
        }
        return null;
    }
}
